package com.hax2max.control;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.RelativeLayout;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;

import com.hax2max.model.Pair;

public class MultipleChildViewExpandableListAdapter extends BaseExpandableListAdapter{
	/*
	 * Copyright (C) 2006 The Android Open Source Project
	 *
	 * Licensed under the Apache License, Version 2.0 (the "License");
	 * you may not use this file except in compliance with the License.
	 * You may obtain a copy of the License at
	 *
	 *      http://www.apache.org/licenses/LICENSE-2.0
	 *
	 * Unless required by applicable law or agreed to in writing, software
	 * distributed under the License is distributed on an "AS IS" BASIS,
	 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	 * See the License for the specific language governing permissions and
	 * limitations under the License.
	 */




	/**
	 * An easy adapter to map static data to group and child views defined in an XML
	 * file. You can separately specify the data backing the group as a List of
	 * Maps. Each entry in the ArrayList corresponds to one group in the expandable
	 * list. The Maps contain the data for each row. You also specify an XML file
	 * that defines the views used to display a group, and a mapping from keys in
	 * the Map to specific views. This process is similar for a child, except it is
	 * one-level deeper so the data backing is specified as a List<List<Map>>,
	 * where the first List corresponds to the group of the child, the second List
	 * corresponds to the position of the child within the group, and finally the
	 * Map holds the data for that particular child.
	 */

	private List<? extends Map<String, ?>> mGroupData;
	private int mExpandedGroupLayout;
	private int mCollapsedGroupLayout;
	private String[] mGroupFrom;
	private int[] mGroupTo;
	private List<? extends List<? extends Map<String, Object>>> mChildData;
	public RelativeLayout actionView = null,infoView = null,callView = null,callView2=null;


	private List<Pair<Integer,Pair<String[],int[]>>> mMapData;

	private LayoutInflater mInflater;


	public MultipleChildViewExpandableListAdapter(Context context,ExpandableListView expandable_dialog_list,
			List<? extends Map<String, ?>> groupData, int groupLayout,
					String[] groupFrom, int[] groupTo,
					List<? extends List<? extends Map<String, Object>>> childData,
							List<Pair<Integer,Pair<String[],int[]>>> childToFrom) {
		this(context, expandable_dialog_list, groupData, groupLayout, groupLayout, groupFrom, groupTo, childData, childToFrom);
	}

	public MultipleChildViewExpandableListAdapter(Context context,ExpandableListView expandable_dialog_list,
			List<? extends Map<String, ?>> groupData, int expandedGroupLayout,
					int collapsedGroupLayout, String[] groupFrom, int[] groupTo,
					List<? extends List<? extends Map<String, Object>>> childData,
							List<Pair<Integer,Pair<String[],int[]>>> childToFrom) {
		mGroupData = groupData;
		mExpandedGroupLayout = expandedGroupLayout;
		mCollapsedGroupLayout = collapsedGroupLayout;
		mGroupFrom = groupFrom;
		mGroupTo = groupTo;

		mChildData = childData;

		mMapData = childToFrom;

		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		infoView = (RelativeLayout)mInflater.inflate(com.hax2max.R.layout.map_info_dialog_child1,expandable_dialog_list,false);
		bindView(infoView, mChildData.get(0).get(0), 
				mMapData.get(0).getSecond().getFirst(), 
				mMapData.get(0).getSecond().getSecond());

		actionView = (RelativeLayout) mInflater.inflate(com.hax2max.R.layout.map_info_dialog_child2,expandable_dialog_list,false);
		bindView(actionView, mChildData.get(1).get(0), 
				mMapData.get(1).getSecond().getFirst(), 
				mMapData.get(1).getSecond().getSecond());

		callView = (RelativeLayout) mInflater.inflate(com.hax2max.R.layout.map_info_dialog_child3,expandable_dialog_list,false);
		bindView(callView, mChildData.get(2).get(0), 
				mMapData.get(2).getSecond().getFirst(), 
				mMapData.get(2).getSecond().getSecond());
		
		callView2 = (RelativeLayout) mInflater.inflate(com.hax2max.R.layout.map_info_dialog_child3,expandable_dialog_list,false);
		bindView(callView2, mChildData.get(2).get(1), 
				mMapData.get(3).getSecond().getFirst(), 
				mMapData.get(3).getSecond().getSecond());
	}

	public Object getChild(int groupPosition, int childPosition) {
		return mChildData.get(groupPosition).get(childPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}


	public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
			View convertView, ViewGroup parent) {

		switch(groupPosition){
		case 0:	return infoView;
		case 1: return actionView;
		case 2: if(childPosition == 0 ) return callView; else if(childPosition == 1) return callView2;
		default: return null;
		}    	
	}


	private void bindView(View view, Map<String, ?> data, String[] from, int[] to) {
		int len = to.length;

		for (int i = 0; i < len; i++) {
			View v = (View)view.findViewById(to[i]);
			if (v != null) {

				/*		if(((String)data.get(from[i])).equals("")){
	            			((RelativeLayout)infoView).removeView(v);
	            		}
	            		else{

	            		}*/
	            		((TextView)v).setText((String)data.get(from[i]));
			}

		}
	}


	public int getChildrenCount(int groupPosition) {

		return mChildData.get(groupPosition).size(); 
	}

	public Object getGroup(int groupPosition) {
		return mGroupData.get(groupPosition);
	}

	public int getGroupCount() {

		return mGroupData.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
			ViewGroup parent) {
		View v;
		if (convertView == null) {
			v = newGroupView(isExpanded, parent);
		} else {
			v = convertView;
		}
		bindView(v, mGroupData.get(groupPosition), mGroupFrom, mGroupTo);
		return v;
	}

	/**
	 * Instantiates a new View for a group.
	 * @param isExpanded Whether the group is currently expanded.
	 * @param parent The eventual parent of this new View.
	 * @return A new group View
	 */
	public View newGroupView(boolean isExpanded, ViewGroup parent) {
		return mInflater.inflate((isExpanded) ? mExpandedGroupLayout : mCollapsedGroupLayout,
				parent, false);
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return super.areAllItemsEnabled();
	}



}
